package com.loveyukari.service;



import com.loveyukari.douyu.utils.DouyuMessageUtil;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

public class NewDouyuClient extends WebSocketClient {

//    private Logger logger = LoggerFactory.getLogger(NewDouyuClient.class);

    public NewDouyuClient(URI serverUri) {
        super(serverUri);
    }

    public NewDouyuClient(URI serverUri, Map<String, String> httpHeaders) {
        super(serverUri, httpHeaders);
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        System.out.println("open");
//        Map<String, Object> loginParam = new HashMap<String,Object>();
//        loginParam.put("type", "loginreq");
//        loginParam.put("roomid", "196");
//        byte[] loginRequestData = DouyuMessageUtil.getRequestData(loginParam);
//        Map<String, Object> groupParam = new HashMap<String,Object>();
//        groupParam.put("type", "joingroup");
//        groupParam.put("rid", "196");
//        groupParam.put("gid", -9999);
//        byte[] groupRequestData = DouyuMessageUtil.getRequestData(groupParam);
//        this.send(loginRequestData);
//        this.send(groupRequestData);
    }

    @Override
    public void onMessage(String s) {
        System.out.println(s);
    }

    @Override
    public void onClose(int i, String s, boolean b) {
        System.out.println("close");
    }

    @Override
    public void onError(Exception e) {
        e.printStackTrace();
    }

    public static void main(String[] args) throws URISyntaxException, InterruptedException {
        HashMap<String, String> httpHeads = new HashMap<>();
        httpHeads.put("Origin","https://www.douyu.com");
        NewDouyuClient douyuClient = new NewDouyuClient(new URI("wss://wsproxy.douyu.com:6671"));
        douyuClient.connect();
        Thread.sleep(10000);
    }
}
